package com.sunny.hp.common.view;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.sunny.hp.common.util.LogUtil;
import com.sunny.hp.common.util.PropertiesUtil;


public class ExcelFormView extends GenericExcelView{//AbstractExcelView {

	protected Logger logger = LogUtil.getLogger(getClass());

	private String templateFilePath = PropertiesUtil.getMessage("exceldown.excelTemplate") ; 
	//buildExcelDocument
	/** 엑셀 파일 다운로드
	 * <PRE>
	 * 1. MethodName	: ExcelDocument
	 * 2. ClassName		: ExcelFormView
	 * 3. Commnet		: 
	 * 4. 작성자			: P044826
	 * 5. 작성일			: 2013. 11. 20. 오후 2:13:46
	 * </PRE>
	 * 		@param model
	 * 		@param wBook
	 * 		@param request
	 * 		@param response
	 * 		@throws Exception
	 */
	@Override
	protected void ExcelDocument(Map model, HSSFWorkbook wBook, HttpServletRequest request,
			HttpServletResponse response, String templateFileName) throws Exception {



		ArrayList<FileInputStream> inList = new ArrayList<FileInputStream>();
		FileInputStream in = new FileInputStream(templateFilePath + model.get("TEMPLATE_FILE_NAME"));
		inList.add(in);
		wBook = new CopySheets().mergeExcelFiles(wBook, inList);

		LinkedHashMap<String, ExcelBaseInfo> excelBaseInfoMap = (LinkedHashMap<String, ExcelBaseInfo>) model.get("BASE_INFO");
		String FileName = new String((model.get("FILE_NAME")+"").getBytes("euc-kr"),"8859_1");

		response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition", "attachment; filename="+ FileName+".xls");

		Set<String> keys = excelBaseInfoMap.keySet();



		for(String key : keys){
			Sheet sh = wBook.getSheet(key);  
			ExcelBaseInfo info = excelBaseInfoMap.get(key);
			LinkedHashMap<String, String > sConditionMapInfo = info.getsConditionMap();

			int searhStartInt = info.getSearchConditionRow();

			//System.out.print("info.getTitleName() ==> " + info.getTitleName());
			sh.getRow(info.getTitleRow()).getCell(2).setCellValue(info.getTitleName());
			Set<String> keys2 = sConditionMapInfo.keySet();
			int i = 0;
			for(String key2 : keys2){
				Row r1 = sh.getRow(searhStartInt++);
				Cell cel = r1.getCell(2);
				cel.setCellValue(sConditionMapInfo.get(key2));
			}
			
			
			int dataRow = info.getDataFirstRow();
			List<LinkedHashMap> dataList = info.getDataList();
			
			if(null != dataList && 0 < dataList.size()){
				int columnSize = dataList.get(0).keySet().size();
				String [] columnKey = new String[columnSize];
				CellStyle [] cellStyle = new  CellStyle[columnSize];
				
				Row r2 = sh.getRow(info.getDataFirstRow());
				
				for(int k = 0 ; k < columnSize ; k++){
					cellStyle[k] = r2.getCell(k+1).getCellStyle();
				}
				int sheetCount = 2;
				for(LinkedHashMap map : dataList){
					if(60000< dataRow){
						sh = wBook.createSheet(key+ sheetCount);
						sheetCount++;
						dataRow = 0;
					}
					Set<String> key_data_list = map.keySet();
					int k = 0;
					sh.createRow(dataRow);
					for(String key_data : key_data_list){
						sh.getRow(dataRow).createCell(k+1).setCellStyle(cellStyle[k]);
						sh.getRow(dataRow).getCell(k+1).setCellValue(map.get(key_data) + "");
						k++;
					}
					dataRow++;
					
				}
			}
		}
	}
}
